<template>
  <el-card style="margin: 10px 10px; height: 70px">
    <el-form class="form1">
      <el-form-item label="角色昵称：">
        <el-input
          width="300px"
          v-model="searchInputVal"
          @input="searchRole"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :disabled="searchInputVal.trim().length === 0"
          @click="searchRole"
          >搜索</el-button
        >
        <el-button type="primary" @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin: 10px 10px; height: 500px">
    <div style="margin: 0 0 10px 0">
      <el-button type="primary" @click="addRole">添加角色</el-button>
    </div>

    <el-table border :data="roleInfoList" style="height: 380px" ref="tableRef">
      <el-table-column type="index" width="55" align="center" label="#" />
      <el-table-column
        label="id"
        prop="id"
        width="80"
        align="center"
      ></el-table-column>

      <el-table-column
        label="角色昵称"
        prop="roleName"
        align="center"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        prop="createTime"
        align="center"
      ></el-table-column>
      <el-table-column
        label="更新时间"
        prop="updateTime"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" width="400px" align="center">
        <template #="{ row, $index }">
          <el-button
            type="primary"
            icon="User"
            size="small"
            @click="assignAuthority(row.id)"
            >分配权限</el-button
          >
          <el-button
            type="primary"
            icon="Edit"
            size="small"
            @click="editRole(row.id, row.roleName)"
            >编辑</el-button
          >
          <el-button
            type="primary"
            icon="Delete"
            size="small"
            @click="delRole(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 10px"
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 5, 7, 10]"
      :background="true"
      layout=" prev, pager, next, jumper,->, sizes,total"
      :total="totalPage"
      @current-change="getRoleInfoList"
      @size-change="getRoleInfoList"
    />
  </el-card>
  <el-dialog v-model="dialogVisible" title="添加角色" width="500">
    <el-form :model="roleInfo" :rules="rules" ref="form2Ref">
      <el-form-item prop="roleName" label="角色名称">
        <el-input v-model="roleInfo.roleName"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveRoleInfo"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
  <drawer-info
    @close="drawerCallback"
    :drawerVisible="drawerVisible"
    :role-id="roleId"
  ></drawer-info>
</template>

<script setup lang="ts">
import { nextTick, onMounted, reactive, ref } from "vue";
import {
  reqAddRole,
  reqDelRole,
  reqGetRoleList,
  reqUpdateRole,
} from "../../../api/acl/role";
import { endLoading, startLoading } from "../../../utils/loading";
import { ElMessage } from "element-plus";
import confirm from "../../../utils/confirm";
import { reqGetPermissionList } from "../../../api/acl/menu";
import DrawerInfo from "./drawerInfo.vue";
const rules = {
  roleName: [{ required: true, min: 2, max: 10, trigger: "blur" }],
};
const roleId = ref(0);
const drawerVisible = ref(false);
const totalPage = ref(100);
const currentPage = ref(1);
const pageSize = ref(8);
const roleInfoList = ref([]);
const searchInputVal = ref("");
const dialogVisible = ref(false);
const roleInfo = reactive({ roleName: "" });
const form2Ref = ref();
const permissionList = ref();

const getRoleInfoList = async () => {
  let result = await reqGetRoleList(currentPage.value, pageSize.value);
  roleInfoList.value = result.data.records;
  totalPage.value = result.data.total;
};
const assignAuthority = (id: number) => {
  roleId.value = id;
  drawerVisible.value = true;
};
const delRole = (id: number) => {
  confirm("你确定要删除吗?", async () => {
    let result = await reqDelRole(id);
    if (result.code === 200) {
      ElMessage.success("删除成功");
      getRoleInfoList();
    } else {
      ElMessage.error("删除失败");
    }
  });
};
const searchRole = async () => {
  if (searchInputVal.value.trim().length > 0) {
    startLoading();
    let result = await reqGetRoleList(currentPage.value, pageSize.value, {
      roleName: searchInputVal.value.trim(),
    });
    endLoading();
    roleInfoList.value = result.data.records;
    totalPage.value = result.data.total;
  } else {
    startLoading();
    getRoleInfoList();
    endLoading();
  }
};
const resetSearch = () => {
  searchInputVal.value = "";
  getRoleInfoList();
};
const addRole = () => {
  dialogVisible.value = true;

  nextTick(() => {
    form2Ref.value.resetFields();
    Object.assign(roleInfo, { id: "", roleName: "" });
  });
};

const saveRoleInfo = () => {
  form2Ref.value.validate(async (valid: any) => {
    let result;
    if (roleInfo.id) {
      result = await reqUpdateRole(roleInfo);
    } else {
      result = await reqAddRole(roleInfo);
    }
    if (result.code === 200) {
      ElMessage.success("成功");
      getRoleInfoList();
      dialogVisible.value = false;
    } else {
      ElMessage.error("失败");
    }
  });
};

const editRole = (id: number, roleName: string) => {
  dialogVisible.value = true;
  nextTick(() => {
    form2Ref.value.resetFields();
    Object.assign(roleInfo, { id, roleName });
  });
};

const getPermissionList = async () => {
  let result = await reqGetPermissionList();
  permissionList.value = result.data;
};

onMounted(() => {
  getRoleInfoList();
  getPermissionList();
});

const drawerCallback = () => {
  drawerVisible.value = false;
  window.location.reload();
};
</script>

<style lang="scss" scoped>
.form1 {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
